package com.hhtc.jsj.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhtc.jsj.common.response.Result;
import com.hhtc.jsj.common.shiro.AccountProfile;
import com.hhtc.jsj.common.shiro.AccountRealm;
import com.hhtc.jsj.common.utils.ShiroUtil;
import com.hhtc.jsj.entity.User;
import com.hhtc.jsj.mapper.UserMapper;
import com.hhtc.jsj.request.UserRequest;
import com.hhtc.jsj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ty
 * @since 2022-04-07
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    @Transactional
    public boolean add(UserRequest userRequest) {
        userRequest.setId(IdWorker.getId());
        userRequest.setCreatedTime(System.currentTimeMillis());
        userRequest.setUpdatedTime(System.currentTimeMillis());
        User user = BeanUtil.copyProperties(userRequest, User.class);
        return this.save(user);
    }

    @Override
    @Transactional
    public boolean remove(UserRequest userRequest) {
        Long id = userRequest.getId();
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean update(UserRequest userRequest) {
        userRequest.setUpdatedTime(System.currentTimeMillis());
        User user = BeanUtil.copyProperties(userRequest, User.class);
        return this.updateById(user);
    }

    @Override
    public UserRequest getById(UserRequest userRequest) {
        Long id = userRequest.getId();
        User user = this.getById(id);
        if (user != null) {
            return BeanUtil.copyProperties(user, UserRequest.class);
        }
        return null;
    }

    @Override
    public List<UserRequest> queryList(UserRequest userRequest) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        this.buildWrapper(queryWrapper, userRequest);
        List<User> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return BeanUtil.copyToList(list, UserRequest.class);
    }

    @Override
    public Page queryPage(UserRequest userRequest) {
        Page<User> page = new Page<>();
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        buildWrapper(wrapper, userRequest);
        Page<User> pageResult = this.page(page, wrapper);
        return pageResult;
    }
    private void buildWrapper(QueryWrapper<User> wrapper, UserRequest param) {
        log.info("buildPageWrapper param :{}", param);
        Long id = param.getId();
        if (id != null) {
            wrapper.eq("id", id);
        }
        String username = param.getUsername();
        if (StringUtils.isNotBlank(username)) {
            wrapper.eq("username", username);
        }
        String userCode = param.getUserCode();
        if (StringUtils.isNotBlank(userCode)) {
            wrapper.eq("user_code", userCode);
        }
        String Password = param.getPassword();
        if (StringUtils.isNotBlank(Password)) {
            wrapper.eq("password", Password);
        }
        String headImage = param.getHeadImage();
        if (StringUtils.isNotBlank(headImage)) {
            wrapper.eq("head_image", headImage);
        }
        String phone = param.getPhone();
        if (phone != null) {
            wrapper.eq("phone", phone);
        }
        String mail = param.getMail();
        if (StringUtils.isNotBlank(mail)) {
            wrapper.eq("mail", mail);
        }
        String address = param.getAddress();
        if (StringUtils.isNotBlank(address)) {
            wrapper.eq("address", address);
        }
        String userRole = param.getUserRole();
        if (userRole != null) {
            wrapper.eq("user_type", userRole);
        }
    }
}
